﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using LabMS.BLL;
using LabMS.Model;
using System.Collections.Generic;
using ExtendBLL;
using System.IO;

namespace LabMS.Web.Equipment.LowValue
{
    public partial class addlowvalue : Base.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            strPagePrivilege.Add("DZPGL");//必须写在ispostback外
            strPageUserType.Add("mana");

            tbProductionDate.Attributes.Add("onclick", "c.showMoreDay = false;c.show(this);");
            tbPurchaseDate.Attributes.Add("onclick", "c.showMoreDay = false;c.show(this);");

            cbcnub.Attributes.Add("onclick", "cbClick()");
            tbKindNum.Attributes.Add("readOnly", "-1");
            tbInstrumentName.Attributes.Add("readOnly", "-1");
            tbStateCode.Style.Add("display", "none");
            tbCountry.Attributes.Add("readOnly", "-1");
            tbPurchaseDate.Attributes.Add("readOnly", "-1");
            tbCreatedDate.Attributes.Add("readOnly", "-1");
            if (!Page.IsPostBack)
            {
                bind();
            }
        }

        public void bind()
        {
            tbCreatedDate.Text = DateTime.Today.ToLongDateString();
            ExtendBLL.Dictionary eDictionary = new ExtendBLL.Dictionary();
            List<Model.Dictionary> listCClass = new List<LabMS.Model.Dictionary>();
            listCClass = eDictionary.GetItemListByDClass("haocaileibie");
            ddlCClass.DataSource = listCClass;
            ddlCClass.DataTextField = "Name";
            ddlCClass.DataValueField = "Code";
            ddlCClass.DataBind();

            listCClass = eDictionary.GetItemListByDClass("shebeilaiyuan");
            ddlSource.DataSource = listCClass;
            ddlSource.DataTextField = "Name";
            ddlSource.DataValueField = "Code";
            ddlSource.DataBind();

            listCClass = eDictionary.GetItemListByDClass("shiyongfangxiang");
            ddlArrow.DataSource = listCClass;
            ddlArrow.DataTextField = "Name";
            ddlArrow.DataValueField = "Code";
            ddlArrow.DataBind();

            listCClass = eDictionary.GetItemListByDClass("jingfeikemu");
            ddlFundsSubject.DataSource = listCClass;
            ddlFundsSubject.DataTextField = "Name";
            ddlFundsSubject.DataValueField = "Code";
            ddlFundsSubject.DataBind();

            listCClass = eDictionary.GetItemListByDClass("dangwei");
            ddlModle.DataSource = listCClass;
            ddlModle.DataTextField = "Name";
            ddlModle.DataValueField = "Code";
            ddlModle.DataBind();


            //LabMS.BLL.Lab blab = new LabMS.BLL.Lab();
            //List<LabMS.Model.Lab> mLab = new List<LabMS.Model.Lab>();
            //mLab = blab.GetModelList(" Lab_ParentID is null ");
            //ddlAddress.DataSource = mLab;
            //ddlAddress.DataTextField = "Lab_Name";
            //ddlAddress.DataValueField = "Lab_Code";
            //ddlAddress.DataBind();

            ExtendBLL.InstrumentAndConsumables eConsum = new ExtendBLL.InstrumentAndConsumables();
            int iMaxCID = eConsum.GetMaxId();
            MaxCID.Value = iMaxCID.ToString("D10");
            tbInstrumentNum.Text = iMaxCID.ToString("D10");
        }


        protected void lbSave_Click(object sender, EventArgs e)
        {
            Model.InstrumentAndConsumables instrument = new LabMS.Model.InstrumentAndConsumables();
            BLL.InstrumentAndConsumables binstrument = new LabMS.BLL.InstrumentAndConsumables();
            instrument.Arrow = ddlArrow.SelectedValue;
            instrument.ClassNumber = ddlCClass.SelectedValue;
            instrument.ClassNumber = tbKindNum.Text.Trim();
            instrument.CountryCode = tbCountry.Text;
            instrument.CreatedDate = DateTime.Today;
            instrument.Documents = tbDocuments.Text.Trim();
            instrument.FactoryCode = tbFactoryCode.Text.Trim();
            instrument.FundsSubject = ddlFundsSubject.SelectedValue;
            instrument.Measurement = ddlModle.SelectedValue;
            instrument.Model = tbModel.Text.Trim();
            instrument.Name = tbInstrumentName.Text.Trim();
            instrument.Numbers = tbInstrumentNum.Text.Trim();
            instrument.OperatorDate = DateTime.Now;
            instrument.Standards = tbStandards.Text;
            instrument.ResponesoblePerson = tbResponesoblePerson.Text.Trim();
            instrument.ContactPhone = tbContactPhone.Text.Trim();
            DateTime tempTime = new DateTime();
            if (DateTime.TryParse(tbProductionDate.Text.Trim(), out tempTime))
            {
                instrument.ProductionDate = tempTime;
            }
            if (DateTime.TryParse(tbPurchaseDate.Text.Trim(), out tempTime))
            {
                instrument.PurchaseDate = tempTime;
            }
            decimal iTemp = 0;
            if (decimal.TryParse(tbAmount.Text.Trim(), out iTemp))
            {
                instrument.Quantity = iTemp;
            }
            instrument.PresentSituation = ddlPresentSituation.SelectedValue;
            instrument.Source = ddlSource.SelectedValue;
            instrument.SpaceLocation = tbLocationCode.Text;
            instrument.StockLocation = tbLocationCode.Text;
            instrument.Type = "lowvalue";
            instrument.Purpose = tbPurpose.Text;
            instrument.UnitPrice = tbUnitPrice.Text.Trim();
            instrument.Status = "正常";
            //上传图片
            try
            {
                if (imageUpload.HasFile)
                {
                    HttpPostedFile upPhoto = imageUpload.PostedFile;
                    int upPhotoLength = upPhoto.ContentLength;
                    string PhotoContentType = upPhoto.ContentType;
                    byte[] PhotoArray = new byte[upPhotoLength];
                    Stream PhotoStream = upPhoto.InputStream;
                    PhotoStream.Read(PhotoArray, 0, upPhotoLength);

                    instrument.Pic = PhotoArray;//相片
                    instrument.PicType = PhotoContentType;//图片类型
                }
            }
            catch
            {
                LabMS.Common.JShelper.JSAlert(Page, "Picwrong", "程序错误:图片保存出错");
            }
            //无供应商
            try
            {
                binstrument.Add(instrument);
                LabMS.Common.JShelper.JSAlertAndRedirect(Page, "AddSuccess", "添加成功!", "addlowvalue.aspx");
            }
            catch (Exception ex)
            {
                lbErr.Visible = true;
                lbErr.Text = ex.Message.ToString();
            }
        }
    }
}
